package DP;

import org.junit.Test;

import java.util.Arrays;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DP
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-22  12:47
 * @Description: TODO
 * @Version: 1.0
 */
public class 最小花费爬楼梯746 {

    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        if (cost.length == 1) {
            return cost[0];
        }
        if (cost.length == 2) {
            return Math.min(cost[0], cost[1]);
        }
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i <= n; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        System.out.println(Arrays.toString(dp));
        return dp[n];
    }

    // 不适用一维数组来记录，而是使用变量分别记之前的数据
    public int minCostClimbingStairs2(int[] cost) {
        int n = cost.length;
        int prev = 0, curr = 0;
        for (int i = 2; i <= n; i++) {
            int next = Math.min(curr + cost[i - 1], prev + cost[i - 2]);
            prev = curr;
            curr = next;
        }
        return curr;
    }

    @Test
    public void test() {
        int i = minCostClimbingStairs(new int[]{10, 15, 20});
        System.out.println(i);
    }
}
